package com.example.itshome.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.itshome.R;
import com.example.itshome.adapter.MainPageFragmentAdapter;
import com.example.itshome.fragment.SignedUpActivityItemFragment;

import java.util.ArrayList;
import java.util.List;

public class SignedUpActivityPageActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView backImageView;
    private ViewPager signUpActivityViewPager;
    private TextView[] pageChoseTextViews;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signed_up_activity_page);
        initFind();
        initTextView();
        initButton();
        initViewPager();
    }

    void initFind(){
        backImageView=findViewById(R.id.imv_signed_up_page_back);
        signUpActivityViewPager=findViewById(R.id.vp_signed_up_activity_page);
    }

    private void initTextView(){
        pageChoseTextViews=new TextView[2];
        pageChoseTextViews[0]=findViewById(R.id.txv_sign_up_activity_page_item1);
        pageChoseTextViews[1]=findViewById(R.id.txv_sign_up_activity_page_item2);
        pageChoseTextViews[0].setTag(1);
        pageChoseTextViews[1].setTag(2);
        pageChangeTo(0);
    }

    private void initButton(){
        backImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        pageChoseTextViews[0].setOnClickListener(this);
        pageChoseTextViews[1].setOnClickListener(this);
    }

    void initViewPager(){
        List<Fragment> fragments=new ArrayList<>();
        fragments.add(new SignedUpActivityItemFragment());
        fragments.add(new SignedUpActivityItemFragment());
        Bundle b1=new Bundle();
        Bundle b2=new Bundle();
        b1.putString("pageType", SignedUpActivityItemFragment.ActivityPageType.ALL.name());
        b2.putString("pageType", SignedUpActivityItemFragment.ActivityPageType.OVER.name());
        fragments.get(0).setArguments(b1);
        fragments.get(1).setArguments(b2);
        signUpActivityViewPager.setAdapter(new MainPageFragmentAdapter(getSupportFragmentManager(),fragments));
        signUpActivityViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                pageChangeTo(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void pageChangeTo(int position){
        for(TextView textView:pageChoseTextViews){
            textView.setSelected(textView.getTag().equals(position+1));
        }
    }


    @Override
    public void onClick(View view) {
        int vid=(int)view.getTag();
        pageChangeTo(vid-1);
        signUpActivityViewPager.setCurrentItem(vid-1);
    }
}